﻿
using HBase;
using System;
using System.Collections.Generic;
using System.Web.Http;

namespace hCommand {
    [RoutePrefix("Task")]
    public class TaskServer :BaseApiController {
        ICoreTaskServer coreTaskServer = WMSDalFactray.getDal<ICoreTaskServer>();


        ///Task/getComplete
        [HttpGet, Route("getComplete")]
        public BaseResult getComplete() {
            return BaseResult.Ok(coreTaskServer.GetAll<CoreTask>("and  status=2"));
        }

        [HttpGet, Route("GetCompleteParam/{id}")]
        public BaseResult GetCompleteParam(long id) {
            return BaseResult.Ok(MyCache.query<CoreTaskParam>((a) => {
                return a.taskId == id;
            }));
        }
        [HttpGet, Route("finishTask/{stn}/{taskNo}")]
        public BaseResult finishTask(int taskNo ,int stn) {
            Node node = PathFacade.getConfig(stn);
            CoreTaskParam p1 = getCompleteParamByTaskNo(taskNo);
            if( node == null ) {
                return BaseResult.Ok("站台不存在");
            } else {

                bool arived = PathFacade.isArived(stn ,p1.des ,null);
                if( !arived ) {
                    return BaseResult.Ok("路径不可达" + stn + "=>" + p1.des);
                }
            }
            int connt = coreTaskServer.updateParamAt(p1.id ,stn);
            CoreTaskParam p = MyCache.getValue<CoreTaskParam>(p1.id);
            if( p.des == stn ) {
                return BaseResult.Ok(connt == 1);
            }

            return BaseResult.Ok(connt == 1);
        }


        [HttpGet, Route("getComplete/{taskNo}")]
        public BaseResult getComplete(int taskNo) {
            CoreTaskParam p1 = getCompleteParamByTaskNo(taskNo);
            CoreTask complete = coreTaskServer.getById<CoreTask>(p1.taskId);
            return BaseResult.Ok(complete);
        }
        [HttpGet, Route("move/{src}/{des}")]
        public BaseResult move(int src ,int des) {
            WMSBaseTransFacade.doWcsMoveTask(WMSIdUtil.getId() ,WMSIdUtil.getId() ,src ,des);
            return BaseResult.Ok("任务生成");
        }

        [HttpGet, Route("DeleteCraneAndCarrierTask/{taskNo}")]
        public bool DeleteCraneAndCarrierTask(int taskNo) {

            CoreTaskParam p = MyCache.queryUnique<CoreTaskParam>((a) => {
                return a.taskNo == taskNo && a.type != 9;
            } ,"CompleteParam 过多" + taskNo);
            long paramId = p.id;
            TransRes res = WMSBaseTransFacade.doDeleteTaskEngine(paramId);
            if( res.s ) {
                PathFacade.removeComplete(p.taskId);

            }
            return true;

        }
        static public CoreTaskParam getCompleteParamByTaskNo(int taskNo) {
            return MyCache.queryUnique<CoreTaskParam>((a) => {
                return a.taskNo == taskNo && a.type != 9;
            } ,"CompleteParam 过多" + taskNo);

        }
        [HttpGet, Route("getTaskByCompleteId/{deviceType}/{id}")]
        public BaseResult getTaskByCompleteId(String deviceType ,long id) {
            Type t = PathFacade.getType(deviceType);
            return BaseResult.Ok(PathFacade.getTaskByCompleteId(t ,id));
        }
        [HttpGet, Route("carrier/{taskNos}")]
        public BaseResult getCarrierkBytaskNo(String taskNos) {
            String[] taskArr = taskNos.Split(',');
            List<TaskCarrier> list = new List<TaskCarrier>();
            foreach( String t in taskArr ) {
                TaskCarrier taskCarrier = PathFacade.getTaskByTaskNo<TaskCarrier>(int.Parse(t));
                if( taskCarrier != null ) {
                    list.Add(taskCarrier);
                }

            }

            return BaseResult.Ok(list);
        }

        [HttpGet, Route("getAllTaskByCompleteId/{id}")]
        public BaseResult getAllTaskByCompleteId(long id) {
            List<Type> types = PathFacade.getAllTaskType();
            Dictionary<String ,List<Object>> dict = new Dictionary<string ,List<object>>();
            foreach( Type t in types ) {
                List<BaseTask> list = PathFacade.getTaskByCompleteId(t ,id);
                String name = t.Name;
                List<object> res = new List<object>();
                foreach( BaseTask bt in list ) {


                    res.Add(bt.Clone());
                }
                dict.Add(name ,res);
            }
            List<CoreTaskParam> paramList = MyCache.query<CoreTaskParam>((a) => {
                return a.taskId == id;
            });
            ;
            List<object> param1 = new List<object>();
            foreach( CoreTaskParam p in paramList ) {
                param1.Add(p);
            }
            dict.Add("completeParam" ,param1);
            return BaseResult.Ok(dict);
        }





        [HttpGet, Route("t/{taskNo}")]
        public BaseResult t(int taskNo) {

            return BaseResult.Ok(MyCache.query<Estimate>(a => {
                return a.taskNo == taskNo;
            }));
        }
    }
}
